//
//  HMEditCartTableViewCell.m
//  HMMarket
//
//  Created by maimaigo3 on 2017/5/9.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMEditCartTableViewCell.h"
#import "CartModel.h"
@interface HMEditCartTableViewCell ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *selectBtn;

@property (weak, nonatomic) IBOutlet UIImageView *titleImageVI;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UITextField *countLabel;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (weak, nonatomic) IBOutlet UIButton *reduceBtn;
@property (weak, nonatomic) IBOutlet UILabel *attr_Label;

@end

@implementation HMEditCartTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [_addBtn addTarget:self action:@selector(addNumber) forControlEvents:UIControlEventTouchUpInside];
    [_addBtn setBackgroundImage:[UIImage imageNamed:@"Cart_add_hightlight"] forState:UIControlStateNormal];
    
    [_reduceBtn addTarget:self action:@selector(reduceNumber) forControlEvents:UIControlEventTouchUpInside];
    [_reduceBtn setBackgroundImage:[UIImage imageNamed:@"Cart_reduce_hightlight"] forState:UIControlStateNormal];

    
    [_selectBtn addTarget:self action:@selector(changeSelectState) forControlEvents:UIControlEventTouchUpInside];
    
    [_selectBtn setImage:[UIImage imageNamed:@"Cart_disSelect"] forState:UIControlStateNormal];
    [_selectBtn setImage:[UIImage imageNamed:@"Cart_select"] forState:UIControlStateSelected];
    
    _countLabel.backgroundColor = kf5f5f5;
    _countLabel.delegate = self;
    _countLabel.userInteractionEnabled = NO;
}

//- (void)setCartModel:(CartModel *)cartModel {
//    _nameLabel.text = cartModel.item_name;
//    _priceLabel.text = [NSString stringWithFormat:@"¥%@元/斤", cartModel.item_price];
//    _countLabel.text = cartModel.item_number;
//    NSString * imgStr = [NSString stringWithFormat:@"%@%@",kImageUrl,cartModel.sm_logo];
//    [_titleImageVI sd_setImageWithURL:[NSURL URLWithString:imgStr] placeholderImage:[UIImage imageNamed:@"Cart_placeholder"] options:SDWebImageRetryFailed|SDWebImageLowPriority];
//}

- (void)setDataArray:(NSMutableArray *)dataArray {
    _dataArray = dataArray;
    _cartModel = _dataArray[_index.section];
    _nameLabel.text = _cartModel.item_name;
    _selectBtn.selected = _cartModel.select;
    _priceLabel.text = [NSString stringWithFormat:@"¥%@/%@", _cartModel.item_price, _cartModel.weight];
    _countLabel.text = _cartModel.item_number;
    _attr_Label.text = _cartModel.item_attr;
    NSString * imgStr = [NSString stringWithFormat:@"%@%@",kImageUrl,_cartModel.sm_logo];
    [_titleImageVI sd_setImageWithURL:[NSURL URLWithString:imgStr] placeholderImage:[UIImage imageNamed:@"Cart_placeholder"] options:SDWebImageRetryFailed|SDWebImageLowPriority];
    
    if ([_cartModel.item_number integerValue] <= 1) {
        [_reduceBtn setBackgroundImage:[UIImage imageNamed:@"Cart_reduce_low"] forState:UIControlStateNormal];
        [HMHUDManager showHUDWithText:@""];
    } else {
        [_reduceBtn setBackgroundImage:[UIImage imageNamed:@"Cart_reduce_hightlight"] forState:UIControlStateNormal];
    }
}

//- (void)setSelectAll:(BOOL)selectAll {
//    _selectBtn.selected = selectAll;
//    for (int i = 0; i < _dataArray.count; i++) {
//        CartModel *dataModel = _dataArray[i];
//        dataModel.select = selectAll;
//    }
//}

- (void)addNumber {
    [HttpBaseTool post:KAddNumber params:@{@"user_tel":[MGAccountTool getTel], @"item_id":_cartModel.id, @"cid":_cartModel.cid} success:^(id responseObj) {
        if ([responseObj[@"code"] integerValue] == 200) {
            NSInteger a = [_countLabel.text floatValue];
            a ++;
            _countLabel.text = [NSString stringWithFormat:@"%ld", a];
            _cartModel.item_number = _countLabel.text;
            if ([_delegate respondsToSelector:@selector(changeNumber:)]) {
                [_delegate changeNumber:ADD_numner];
            }
            [_reduceBtn setBackgroundImage:[UIImage imageNamed:@"Cart_reduce_hightlight"] forState:UIControlStateNormal];
        } else {
            [HMHUDManager showHUDWithText:responseObj[@"msg"]];
        }
    } failure:^(NSError *error) {
        
    }];
}

- (void)reduceNumber {
    NSInteger a = [_countLabel.text integerValue];
    a --;
    if (a < 1) {
        [_reduceBtn setBackgroundImage:[UIImage imageNamed:@"Cart_reduce_low"] forState:UIControlStateNormal];
    } else {
        
        [HttpBaseTool post:KDeleteNumber params:@{@"cid":_cartModel.cid} success:^(id responseObj) {
            if ([responseObj[@"code"] integerValue] == 200) {
                _countLabel.text = [NSString stringWithFormat:@"%ld", a];
                _cartModel.item_number = _countLabel.text;
                if ([_delegate respondsToSelector:@selector(changeNumber:)]) {
                    [_delegate changeNumber:REDUCE_numer];
                }
                if (a == 1) {
                    [_reduceBtn setBackgroundImage:[UIImage imageNamed:@"Cart_reduce_low"] forState:UIControlStateNormal];
                } else {
                    [_reduceBtn setBackgroundImage:[UIImage imageNamed:@"Cart_reduce_hightlight"] forState:UIControlStateNormal];
                }
            } else {
                [HMHUDManager showHUDWithText:responseObj[@"msg"]];
            }
        } failure:^(NSError *error) {
            
        }];
    }
}

- (void)changeSelectState{
    _selectBtn.selected = !_selectBtn.selected;
    _cartModel.select = _selectBtn.selected;
    if ([_delegate respondsToSelector:@selector(changeStatus:)]) {
        [_delegate changeStatus:_selectBtn.selected];
    }
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    _cartModel = _dataArray[_index.section];
    _cartModel.item_number = textField.text;
    if ([_delegate respondsToSelector:@selector(changeNumber:)]) {
        [_delegate changeNumber:REDUCE_numer];
    }
}

@end
